package spem.activity.diagram.navigator;

import org.eclipse.gmf.runtime.common.ui.services.parser.CommonParserHint;
import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions;
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
import org.eclipse.ui.navigator.ICommonLabelProvider;

import spem.Activity;
import spem.Decision;
import spem.activity.diagram.edit.parts.Activity2EditPart;
import spem.activity.diagram.edit.parts.ActivityEditPart;
import spem.activity.diagram.edit.parts.ActivityNameEditPart;
import spem.activity.diagram.edit.parts.ControlFlowEditPart;
import spem.activity.diagram.edit.parts.DecisionEditPart;
import spem.activity.diagram.edit.parts.RoleDefinitionEditPart;
import spem.activity.diagram.edit.parts.RoleDefinitionNameEditPart;
import spem.activity.diagram.edit.parts.RoleDefinitionPerformsEditPart;
import spem.activity.diagram.edit.parts.Step2EditPart;
import spem.activity.diagram.edit.parts.StepName2EditPart;
import spem.activity.diagram.edit.parts.TaskDefinitionEditPart;
import spem.activity.diagram.edit.parts.TaskDefinitionInputEditPart;
import spem.activity.diagram.edit.parts.TaskDefinitionNameEditPart;
import spem.activity.diagram.edit.parts.TaskDefinitionOutputEditPart;
import spem.activity.diagram.edit.parts.TaskDefinitionUsesEditPart;
import spem.activity.diagram.edit.parts.ToolEditPart;
import spem.activity.diagram.edit.parts.ToolNameEditPart;
import spem.activity.diagram.edit.parts.WorkProductDefinitionEditPart;
import spem.activity.diagram.edit.parts.WorkProductDefinitionNameEditPart;
import spem.activity.diagram.part.SpemDiagramEditorPlugin;
import spem.activity.diagram.part.SpemVisualIDRegistry;
import spem.activity.diagram.providers.SpemElementTypes;
import spem.activity.diagram.providers.SpemParserProvider;

/**
 * @generated
 */
public class SpemNavigatorLabelProvider extends LabelProvider implements
		ICommonLabelProvider, ITreePathLabelProvider {

	/**
	 * @generated
	 */
	static {
		SpemDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?UnknownElement", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
		SpemDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?ImageNotFound", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	public void updateLabel(ViewerLabel label, TreePath elementPath) {
		Object element = elementPath.getLastSegment();
		if (element instanceof SpemNavigatorItem
				&& !isOwnView(((SpemNavigatorItem) element).getView())) {
			return;
		}
		label.setText(getText(element));
		label.setImage(getImage(element));
	}

	/**
	 * @generated
	 */
	public Image getImage(Object element) {
		if (element instanceof SpemNavigatorGroup) {
			SpemNavigatorGroup group = (SpemNavigatorGroup) element;
			return SpemDiagramEditorPlugin.getInstance().getBundledImage(
					group.getIcon());
		}

		if (element instanceof SpemNavigatorItem) {
			SpemNavigatorItem navigatorItem = (SpemNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return super.getImage(element);
			}
			return getImage(navigatorItem.getView());
		}

		return super.getImage(element);
	}

	/**
	 * @generated
	 */
	public Image getImage(View view) {
		switch (SpemVisualIDRegistry.getVisualID(view)) {
		case TaskDefinitionEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?spem?TaskDefinition", SpemElementTypes.TaskDefinition_2005); //$NON-NLS-1$
		case RoleDefinitionPerformsEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?spem?RoleDefinition?performs", SpemElementTypes.RoleDefinitionPerforms_4002); //$NON-NLS-1$
		case TaskDefinitionInputEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?spem?TaskDefinition?input", SpemElementTypes.TaskDefinitionInput_4003); //$NON-NLS-1$
		case RoleDefinitionEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?spem?RoleDefinition", SpemElementTypes.RoleDefinition_2002); //$NON-NLS-1$
		case DecisionEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?spem?Decision", SpemElementTypes.Decision_2004); //$NON-NLS-1$
		case ToolEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?spem?Tool", SpemElementTypes.Tool_2006); //$NON-NLS-1$
		case ControlFlowEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?spem?ControlFlow", SpemElementTypes.ControlFlow_4001); //$NON-NLS-1$
		case Step2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?spem?Step", SpemElementTypes.Step_3001); //$NON-NLS-1$
		case TaskDefinitionUsesEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?spem?TaskDefinition?uses", SpemElementTypes.TaskDefinitionUses_4004); //$NON-NLS-1$
		case WorkProductDefinitionEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?spem?WorkProductDefinition", SpemElementTypes.WorkProductDefinition_2001); //$NON-NLS-1$
		case Activity2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?spem?Activity", SpemElementTypes.Activity_2003); //$NON-NLS-1$
		case ActivityEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Diagram?spem?Activity", SpemElementTypes.Activity_1000); //$NON-NLS-1$
		case TaskDefinitionOutputEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?spem?TaskDefinition?output", SpemElementTypes.TaskDefinitionOutput_4005); //$NON-NLS-1$
		}
		return getImage("Navigator?UnknownElement", null); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private Image getImage(String key, IElementType elementType) {
		ImageRegistry imageRegistry = SpemDiagramEditorPlugin.getInstance()
				.getImageRegistry();
		Image image = imageRegistry.get(key);
		if (image == null && elementType != null
				&& SpemElementTypes.isKnownElementType(elementType)) {
			image = SpemElementTypes.getImage(elementType);
			imageRegistry.put(key, image);
		}

		if (image == null) {
			image = imageRegistry.get("Navigator?ImageNotFound"); //$NON-NLS-1$
			imageRegistry.put(key, image);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public String getText(Object element) {
		if (element instanceof SpemNavigatorGroup) {
			SpemNavigatorGroup group = (SpemNavigatorGroup) element;
			return group.getGroupName();
		}

		if (element instanceof SpemNavigatorItem) {
			SpemNavigatorItem navigatorItem = (SpemNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return null;
			}
			return getText(navigatorItem.getView());
		}

		return super.getText(element);
	}

	/**
	 * @generated
	 */
	public String getText(View view) {
		if (view.getElement() != null && view.getElement().eIsProxy()) {
			return getUnresolvedDomainElementProxyText(view);
		}
		switch (SpemVisualIDRegistry.getVisualID(view)) {
		case TaskDefinitionEditPart.VISUAL_ID:
			return getTaskDefinition_2005Text(view);
		case RoleDefinitionPerformsEditPart.VISUAL_ID:
			return getRoleDefinitionPerforms_4002Text(view);
		case TaskDefinitionInputEditPart.VISUAL_ID:
			return getTaskDefinitionInput_4003Text(view);
		case RoleDefinitionEditPart.VISUAL_ID:
			return getRoleDefinition_2002Text(view);
		case DecisionEditPart.VISUAL_ID:
			return getDecision_2004Text(view);
		case ToolEditPart.VISUAL_ID:
			return getTool_2006Text(view);
		case ControlFlowEditPart.VISUAL_ID:
			return getControlFlow_4001Text(view);
		case Step2EditPart.VISUAL_ID:
			return getStep_3001Text(view);
		case TaskDefinitionUsesEditPart.VISUAL_ID:
			return getTaskDefinitionUses_4004Text(view);
		case WorkProductDefinitionEditPart.VISUAL_ID:
			return getWorkProductDefinition_2001Text(view);
		case Activity2EditPart.VISUAL_ID:
			return getActivity_2003Text(view);
		case ActivityEditPart.VISUAL_ID:
			return getActivity_1000Text(view);
		case TaskDefinitionOutputEditPart.VISUAL_ID:
			return getTaskDefinitionOutput_4005Text(view);
		}
		return getUnknownElementText(view);
	}

	/**
	 * @generated
	 */
	private String getTaskDefinitionOutput_4005Text(View view) {
		IParser parser = SpemParserProvider.getParser(
				SpemElementTypes.TaskDefinitionOutput_4005,
				view.getElement() != null ? view.getElement() : view,
				CommonParserHint.DESCRIPTION);
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SpemDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 6004); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getTaskDefinitionUses_4004Text(View view) {
		IParser parser = SpemParserProvider.getParser(
				SpemElementTypes.TaskDefinitionUses_4004,
				view.getElement() != null ? view.getElement() : view,
				CommonParserHint.DESCRIPTION);
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SpemDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 6003); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getActivity_2003Text(View view) {
		IParser parser = SpemParserProvider.getParser(
				SpemElementTypes.Activity_2003,
				view.getElement() != null ? view.getElement() : view,
				SpemVisualIDRegistry.getType(ActivityNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SpemDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5003); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getTaskDefinition_2005Text(View view) {
		IParser parser = SpemParserProvider.getParser(
				SpemElementTypes.TaskDefinition_2005,
				view.getElement() != null ? view.getElement() : view,
				SpemVisualIDRegistry
						.getType(TaskDefinitionNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SpemDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5004); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getActivity_1000Text(View view) {
		Activity domainModelElement = (Activity) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			SpemDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 1000); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getTool_2006Text(View view) {
		IParser parser = SpemParserProvider.getParser(
				SpemElementTypes.Tool_2006,
				view.getElement() != null ? view.getElement() : view,
				SpemVisualIDRegistry.getType(ToolNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SpemDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5005); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getControlFlow_4001Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getStep_3001Text(View view) {
		IParser parser = SpemParserProvider.getParser(
				SpemElementTypes.Step_3001,
				view.getElement() != null ? view.getElement() : view,
				SpemVisualIDRegistry.getType(StepName2EditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SpemDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5006); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getWorkProductDefinition_2001Text(View view) {
		IParser parser = SpemParserProvider.getParser(
				SpemElementTypes.WorkProductDefinition_2001,
				view.getElement() != null ? view.getElement() : view,
				SpemVisualIDRegistry
						.getType(WorkProductDefinitionNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SpemDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5001); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getRoleDefinitionPerforms_4002Text(View view) {
		IParser parser = SpemParserProvider.getParser(
				SpemElementTypes.RoleDefinitionPerforms_4002,
				view.getElement() != null ? view.getElement() : view,
				CommonParserHint.DESCRIPTION);
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SpemDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 6002); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getDecision_2004Text(View view) {
		Decision domainModelElement = (Decision) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			SpemDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 2004); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getRoleDefinition_2002Text(View view) {
		IParser parser = SpemParserProvider.getParser(
				SpemElementTypes.RoleDefinition_2002,
				view.getElement() != null ? view.getElement() : view,
				SpemVisualIDRegistry
						.getType(RoleDefinitionNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SpemDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5002); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getTaskDefinitionInput_4003Text(View view) {
		IParser parser = SpemParserProvider.getParser(
				SpemElementTypes.TaskDefinitionInput_4003,
				view.getElement() != null ? view.getElement() : view,
				CommonParserHint.DESCRIPTION);
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SpemDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 6001); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getUnknownElementText(View view) {
		return "<UnknownElement Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	private String getUnresolvedDomainElementProxyText(View view) {
		return "<Unresolved domain element Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	public void init(ICommonContentExtensionSite aConfig) {
	}

	/**
	 * @generated
	 */
	public void restoreState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public void saveState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public String getDescription(Object anElement) {
		return null;
	}

	/**
	 * @generated
	 */
	private boolean isOwnView(View view) {
		return ActivityEditPart.MODEL_ID.equals(SpemVisualIDRegistry
				.getModelID(view));
	}

}
